package com.block.merchant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.block.common.exception.NotifyExceptionService;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantDataNoticeLog;
import com.block.db.mapper.MerchantDataNoticeLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.merchant.service.IMerchantDataNoticeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 商户回调通知日录 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-12-09
 */
@Service
public class MerchantDataNoticeLogServiceImpl extends ServiceImpl<MerchantDataNoticeLogMapper, MerchantDataNoticeLog> implements IMerchantDataNoticeLogService {

    @Autowired
    NotifyExceptionService notifyExceptionService;

    @Override
    public MerchantDataNoticeLog saveMerchantNoticeInfo(String noticeType, MerchantBaseInfo merchantBaseInfo, String paramMap, String paramJson){

       try {
           MerchantDataNoticeLog noticeLog = new MerchantDataNoticeLog();
           noticeLog.setNoticeType(noticeType);
           noticeLog.setInfoId(merchantBaseInfo.getId());
           noticeLog.setInfoName(merchantBaseInfo.getInfoName());
           noticeLog.setDateInfo(LocalDate.now().toString());
           noticeLog.setParamMap(paramMap);
           noticeLog.setParamContent(paramJson);

           boolean save = this.save(noticeLog);
           return noticeLog;
       }catch (Exception exception){
           exception.printStackTrace();
           String message = StrUtil.format("【infoId】{}【infoName】{}【paramMap】{}【paramJson】{}",
                   merchantBaseInfo.getId() , merchantBaseInfo.getInfoName(), paramMap, paramJson );
           notifyExceptionService.notifyNoticeInfo("NOTICE_LOG_SAVE", "保存回调通知记录", message, exception );
       }
       return null;
    }

    @Override
    public MerchantDataNoticeLog updateMerchantNoticeInfo(MerchantDataNoticeLog noticeLog, String returnObject){
        try {
            MerchantDataNoticeLog updateEntity = new MerchantDataNoticeLog();
            updateEntity.setId(noticeLog.getId());
            updateEntity.setReturnResult(returnObject);

            boolean save = this.updateById(updateEntity);
            return noticeLog;
        }catch (Exception exception){
            exception.printStackTrace();
            String message = StrUtil.format("【logId】{}【infoId】{}【infoName】{}【returnObject】{}",
                    noticeLog.getId() , noticeLog.getInfoId(), noticeLog.getInfoName(), returnObject );
            notifyExceptionService.notifyNoticeInfo("NOTICE_LOG_UPDATE", "更新回调通知记录", message, exception );
        }
        return null;
    }



}
